/* ---------------------------------------------------------------------------------------------
 *
 *            Capital Alliance Software Confidential Proprietary 
 *            (c) Copyright CAS 201{x}, All Rights Reserved
 *                          www.pekall.com
 *
 * ----------------------------------------------------------------------------------------------
 */
package com.pekall.todo.util;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

public class MyProvider extends ContentProvider {
	ToDoSQLHelper sd;
	private static final UriMatcher sMatcher;
	static {
		sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		sMatcher.addURI(ToDoSQLHelper.AUTHORITY, "todo_tasks",
				ToDoSQLHelper.ITEM);
		sMatcher.addURI(ToDoSQLHelper.AUTHORITY, "todo_tasks/#",
				ToDoSQLHelper.ITEM_ID);
	}

	@Override
	public boolean onCreate() {
		sd = new ToDoSQLHelper(this.getContext());
		return true;
	}

	@Override
	public String getType(Uri uri) {
		switch (sMatcher.match(uri)) {
		case ToDoSQLHelper.ITEM:
			return ToDoSQLHelper.CONTENT_TYPE;
		case ToDoSQLHelper.ITEM_ID:
			return ToDoSQLHelper.CONTENT_ITEM_TYPE;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		SQLiteDatabase sdb = sd.getWritableDatabase();
		long rowId;
		if (sMatcher.match(uri) != ToDoSQLHelper.ITEM) {
			throw new IllegalArgumentException("Unknow URI " + uri);
		}
		rowId = sdb.insert(ToDoSQLHelper.TABLE_NAME, "_id", values);
		if (rowId > 0) {
			Uri noteUri = ContentUris.withAppendedId(ToDoSQLHelper.CONTENT_URI,
					rowId);
			this.getContext().getContentResolver().notifyChange(noteUri, null);
			return noteUri;
		}
		throw new SQLException("Failed to insert row into " + uri);
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		SQLiteDatabase sdb = sd.getWritableDatabase();
		int count;
		switch (sMatcher.match(uri)) {
		case ToDoSQLHelper.ITEM:
			count = sdb.delete(ToDoSQLHelper.TABLE_NAME, selection,
					selectionArgs);
			break;
		case ToDoSQLHelper.ITEM_ID:
			String id = uri.getPathSegments().get(1);
			count = sdb.delete(ToDoSQLHelper.DB_NAME, "_id"
					+ "="
					+ id
					+ (!TextUtils.isEmpty(selection) ? " AND (" + selection
							+ ")" : ""), selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		this.getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteDatabase sdb = sd.getReadableDatabase();
		Cursor c;
		switch (sMatcher.match(uri)) {
		case ToDoSQLHelper.ITEM:
			c = sdb.query(ToDoSQLHelper.TABLE_NAME, projection, selection,
					selectionArgs, null, null, sortOrder);
			break;
		case ToDoSQLHelper.ITEM_ID:
			String id = uri.getPathSegments().get(1);
			c = sdb.query(ToDoSQLHelper.TABLE_NAME, projection, "_id"
					+ "="
					+ id
					+ (!TextUtils.isEmpty(selection) ? " AND (" + selection
							+ ")" : ""), selectionArgs, null, null, sortOrder);
			break;
		default:
			Log.i("sMatcher.match(uri)", String.valueOf(sMatcher.match(uri)));
			throw new IllegalArgumentException("Query with unknown URI: " + uri);
		}
		c.setNotificationUri(getContext().getContentResolver(), uri);
		return c;
	}

	// 更新，与删除类方法类似
	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		SQLiteDatabase sdb = sd.getWritableDatabase();
		int count;
		switch (sMatcher.match(uri)) {
		case ToDoSQLHelper.ITEM:
			count = sdb.update(ToDoSQLHelper.TABLE_NAME, values, selection,
					selectionArgs);
			break;
		case ToDoSQLHelper.ITEM_ID:
			String id = uri.getPathSegments().get(1);
			count = sdb.update(ToDoSQLHelper.DB_NAME, values, "_id"
					+ "="
					+ id
					+ (!TextUtils.isEmpty(selection) ? " AND (" + selection
							+ ")" : ""), selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		this.getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}
}